﻿package b3.widget.card
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	import b3.widget.warehouse.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class ChangeTypePane extends TabController
	{
		private var card:CardModel;
		private var targetBuilding:BuildingModel;
		
		public function ChangeTypePane(card:CardModel, targetBuilding:BuildingModel)
		{
			this.card = card;
			this.targetBuilding = targetBuilding;
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("WarehouseBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBgr1");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("TabTitleBgr2");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage3");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp3");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown3");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage3");
			super(TabControllerBackground, CloseButton);
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			var tab:Tab = new Tab("建筑", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("武器", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("绿化", 4, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(190, 245);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			this.setTabTitlePos(52, -25); 
			this.setTabPagePos(84, 75);
			this.setCloseButtonPos(235, 353);
			//标签图标的事件
			loadBuildingList();
			
			this.x = -this.width / 2;
			this.y = -this.height / 2;
		}
		
		private function loadBuildingList():void
		{
			var buildingConfig:BuildingConfig;
			//所有建筑
			var buildingIndexes:Array = new Array();
			for (var indexStr:String in Config.buildingConfigs)
			{
				buildingConfig = Config.buildingConfigs[indexStr];
				if (buildingConfig.storeTabIndex >= 0)
				{
					buildingIndexes.push(buildingConfig.index);
				}
			}
		    buildingIndexes.sort(sortObjects);
		   	
			for (var i:int = 0; i < buildingIndexes.length; i++)
			{
				var index:int = buildingIndexes[i];
				buildingConfig = Config.buildingConfigs[index];
				if (buildingConfig.type < 3)
				{
					var buildingTypeItem:BuildingTypeItem = new BuildingTypeItem(index);
					buildingTypeItem.addEventListener(MouseEvent.CLICK, onBuildingTypeItemClick);
					this.tabs[buildingConfig.type].addItem(buildingTypeItem, 92, 112);
				}
			}
		}
		
		private function onBuildingTypeItemClick(event:Event):void
		{
			var buildingTypeItem:BuildingTypeItem = event.currentTarget as BuildingTypeItem;
			card.useAlterationCard(buildingTypeItem.buildingIndex);
			close();
		}
		
		//排序
		private function sortObjects(a:int, b:int):int
		{
			var objectConfigA:ObjConfig = Config.buildingConfigs[a];
			var objectConfigB:ObjConfig = Config.buildingConfigs[b];
			if (objectConfigA.sortIndex < objectConfigB.sortIndex)
			{
				return -1;
			}
			else if (objectConfigA.sortIndex > objectConfigB.sortIndex)
			{
				return 1;
			}
			return 0;
		}
	}
}